/*******************************************************************************
 * Copyright (c) 2011-11-10 @author <a href="mailto:iffiff1@hotmail.com">Tyler Chen</a>.
 * All rights reserved.
 *
 * Contributors:
 *     <a href="mailto:iffiff1@hotmail.com">Tyler Chen</a> - initial API and implementation
 ******************************************************************************/
package com.redhat.auth.ejb.impl;

import javax.ejb.Remote;
import javax.ejb.Stateless;

import org.springframework.stereotype.Service;

import com.dayatang.domain.InstanceFactory;
import com.dayatang.querychannel.service.QueryChannelService;
import com.redhat.auth.domain.SecurityStrategy;
import com.redhat.auth.ejb.SecurityStrategyApplication;
import com.redhat.auth.ejb.vo.SecurityStrategyVO;

/**
 * @author <a href="mailto:iffiff1@hotmail.com">Tyler Chen</a> 
 * @since 2011-11-10
 */
@Service("ramsSecurityStrategyApplication")
@Stateless(name = "SecurityStrategyApplicationBean")
@Remote(SecurityStrategyApplication.class)
@javax.interceptor.Interceptors(SpringEJBIntercepter.class)
public class SecurityStrategyApplicationImpl implements
		SecurityStrategyApplication {

	// 查询通道
	protected static QueryChannelService queryChannel;

	protected static QueryChannelService queryChannel() {
		if (queryChannel == null) {
			queryChannel = InstanceFactory
					.getInstance(QueryChannelService.class);
		}
		return queryChannel;
	}

	public void deleteSecurityStrategy(Long id) {
		SecurityStrategy strategy = SecurityStrategy.get(id);
		if (strategy != null) {
			strategy.remove();
		}
	}

	public SecurityStrategyVO loadSecurityStrategy() {
		return Convertor.po2vo(SecurityStrategy.load(), null);
	}

	/**
	 * 保存安全策略设置
	 */
	public SecurityStrategyVO saveSecurityStrategy(SecurityStrategyVO vo) {
		SecurityStrategy strategy = null;
		if (vo == null) {
			return null;
		}
		if (vo.getId() == null) {
			strategy = Convertor.vo2po(vo, new SecurityStrategy());
		} else {
			strategy = SecurityStrategy.get(vo.getId());
		}
		if (strategy == null) {
			return null;
		}
		strategy.save(strategy);
		return Convertor.po2vo(strategy, null);
	}

}
